<?php
class QuestionnaireAction extends Action{

    public function get_questionnaire_list()
    {
        if(!Database($_REQUEST["cid"]))exit;
        $_REQUEST['uid']        = check_null(40002,true,$_REQUEST['uid']);
        //1 没答 2 答过
        $_REQUEST['have_answer']= check_null(40093,true,$_REQUEST['have_answer']);

        $rs = D('Questionnaire')-> get_questionnaire_list($_REQUEST);
        if($rs)
            return_json(0,$rs);
        else
            return_json(40001);
    }

    public function get_questionnaire_question()
    {
        if(!Database($_REQUEST["cid"]))exit;
        $_REQUEST['audience_id']      = check_null(40002,true,$_REQUEST['uid']);
        $_REQUEST['questionnaire_id'] = check_null(40094,true,$_REQUEST['questionnaire_id']);

        $rs['questions'] = D('Questionnaire')-> get_questionnaire_question($_REQUEST);

        $rs['answer']    = D('Questionnaire')-> get_questionnaire_answer($_REQUEST);

        $rs['participate_num'] = D('Questionnaire')->get_audience_num($_REQUEST['questionnaire_id']);

        if($rs)
            return_json(0,$rs);
        else
            return_json(40001);
    }

    public function add_question_answer(){
        $rs = '';
        if(!Database($_REQUEST["cid"]))exit;
        $_REQUEST['uid']             = check_null(40002,true,$_REQUEST['uid']);
        $_REQUEST['questionnaire_id']= check_null(40094,true,$_REQUEST['questionnaire_id']);
        $_REQUEST['answers']         = check_null(40095,true,$_REQUEST['answers']);

        $arr = json_decode($_REQUEST['answers'],true);

        foreach($arr as $val)
        {
            $data['questionnaire_id'] = $_REQUEST['questionnaire_id'];
            $data['audience_id']      = $_REQUEST['uid'];
            $data['question_id']      = $val['question_id'];
            $data['answer']           = isset($val['answer'])?$val['answer']:'';
            $data['other']            = isset($val['other'])?$val['other']:'';
            $rs = D('Questionnaire')-> submit_answers($data);
            $data = null;unset($data);
        }
        if(!$rs)
            return_json(-1);
        else
            return_json(0);
    }
}